//
//  GYShopEvaluateViewController.m
//  GoodLuck
//
//  Created by apple on 2018/2/2.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYShopEvaluateViewController.h"
#import "UIButton+GYExtension.h"
#import "GYEvaluateView.h"
#import "UIImageView+GYExtension.h"
#import "GYNoAddressView.h"
#import "GYNetWorkTools.h"
#import "GYGoodsEvaluateCell.h"


@interface GYShopEvaluateViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 商品评价选择视图 */
@property (nonatomic, weak) GYEvaluateView *evaluateView;

/** 没有评价的视图 */
@property (nonatomic, strong) GYNoAddressView *noGoodsEvaluateView;

/** 数据源 */
@property (nonatomic, strong) NSMutableArray *goodsEvaluateLists;

/** 表格 */
@property (nonatomic, weak) UITableView *evaluateTableview;
@end

@implementation GYShopEvaluateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化UI
    [self setupUI];

    //初始化评价选择视图(由于XIB创建的视图viewDidLoad加载的时候还没有进行布局,所以还有问题)
    [self setupEvaluateView];

    //加载没有商品评论的视图
    [self getNoGoodsEvaluateView];
}
//viewController 将要布局的时候
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    //布局评价选择栏
    self.evaluateView.frame = CGRectMake(0, 0, ScreenWidth, 50);
}


- (void)setupUI {
    self.view.backgroundColor = RGBColor(245, 245, 245);
    
    //1.初始化表格
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, ScreenWidth, ScreenHeight-64-50) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:tableView];
    self.evaluateTableview = tableView;
    
    //加载数据
    [self loadGoodsEvaluateData:@""];
}


- (void)setupEvaluateView {
    GYEvaluateView *evaluateView = [[[NSBundle mainBundle] loadNibNamed:@"GYEvaluateView" owner:nil options:nil] firstObject];
    evaluateView.goodsEvaluateCallBack = ^(NSInteger tag) {
        switch (tag) {
            case 1:
            {
                [self loadGoodsEvaluateData:@""];
            }
                break;
                
            case 2:
            {
                [self loadGoodsEvaluateData:@"1"];
            }
                break;
                
            case 3:
            {
                [self loadGoodsEvaluateData:@"2"];
            }
                break;
                
            case 4:
            {
                [self loadGoodsEvaluateData:@"3"];
            }
                break;
                
            case 5:
            {
                [self loadGoodsEvaluateData:@"4"];
            }
                break;
                
            default:
                break;
        }
    };
    
    [self.view addSubview:evaluateView];
    self.evaluateView = evaluateView;
}

- (void)loadGoodsEvaluateData:(NSString *)type {
    //2.加载评论数据(获取全部评价)
    [EasyLodingView showLodingText:@"正在加载..."];
    [GYNetWorkTools requestGoodsEvaluate:self.goods_id curpage:@"" page:@"" type:type success:^(NSDictionary *obj) {
            //NSLog(@"obj================%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoding];
            NSArray *goods_eval_list = [datas objectForKey:@"goods_eval_list"];
            if (goods_eval_list.count == 0) {
                //代表没有评论
                self.noGoodsEvaluateView.hidden = NO;
            }else {
                //否者加载评论
                self.noGoodsEvaluateView.hidden = YES;
                
                self.goodsEvaluateLists = [GYGoodsEvaluate loadGoodsEvaluateDtaa:goods_eval_list];
                [self.evaluateTableview reloadData];
            }
        }else if (code == 400) {
            [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
            [EasyLodingView hidenLoding];
            NSLog(@"网络加载数据====%@",[datas objectForKey:@"error"]);
        }
    } error:^(NSError *error) {
        [EasyLodingView hidenLoding];
        [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
        NSLog(@"loadGoodsEvaluateFailedError===%@",error);
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.goodsEvaluateLists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYGoodsEvaluateCell *cell = [GYGoodsEvaluateCell goodsEvaluateCellWithTableView:tableView];
    cell.goodsEvaluateModel = self.goodsEvaluateLists[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYGoodsEvaluateCell *cell = [GYGoodsEvaluateCell goodsEvaluateCellWithTableView:tableView];
    
    return [cell cellHeight:self.goodsEvaluateLists[indexPath.item]];
}

#pragma mark -- 创建一个没有记录的视图

- (void)getNoGoodsEvaluateView {
    
    GYNoAddressView *shopCollectionView = [[[NSBundle mainBundle] loadNibNamed:@"GYNoAddressView" owner:nil options:nil] firstObject];
    shopCollectionView.frame = CGRectMake(0, 50, ScreenWidth, self.view.height-50);
    shopCollectionView.markImageView.image = [UIImage imageNamed:@"mcc_04_w"];
    shopCollectionView.headLable.text = @"该商品未收到任何评价";
    shopCollectionView.footLable.text = @"期待您的购买与评论";
    shopCollectionView.hidden = YES;
    shopCollectionView.addBtu.hidden = YES;
    
    [self.view addSubview:shopCollectionView];
    self.noGoodsEvaluateView = shopCollectionView;
}


@end
